import express, { Request, Response } from 'express';
import bodyParser from 'body-parser';
import path from 'path';
import fs from 'fs';
import https from 'https';
import { download720HTTPByVideoLink } from './download_services';
import { getVideoSource, getVideoLink } from './utils';

const app = express();
app.use(bodyParser.json());

// middleware to get video_link from fake_link, attach it to req.query.
app.use((req, res, next) => {
    if(req.query.fake_link)
        req.query.video_link = getVideoLink(req.query.fake_link);
    next(); 
})

// test 
app.get('/', (req: Request, res: Response) => {
    res.header('Access-Control-Allow-Origin', '*');
    res.send("hello, world!");
})

app.post('/', (req: Request, res: Response) => {
    res.send(req.body);
})

// download by http
app.get('/download720HTTP', (req, res) => {
    let video_link = req.query.video_link;
    res.send(video_link);
})

app.listen(3000);